#!/bin/bash
 
#########################################################################
# CreateCollage.sh Ver 0.5                                              #
# Script to Create a collage of images using the specified image set    #
# Created by Suramya Tomar (suramya@suramya.com)                        #
# Last updated 25th Nov 2008                                            #
#-----------------------------------------------------------------------#
# This program is free software; you can redistribute it and/or modify  #
# it under the terms of the GNU General Public License as published by  #
# the Free Software Foundation; either version 2 of the License, or     #
# (at your option) any later version.                                   #
#                                                                       #
# This program is distributed in the hope that it will be useful, but   #
# WITHOUT ANY WARRANTY; without even the implied warranty of            #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      #
# General Public License for more details.                              #
#                                                                       #
# You should have received a copy of the GNU General Public License     #
# along with this program; if not, write to the                         #
# Free Software Foundation, Inc.,                                       #
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                 #
#########################################################################
 
 
if [ $# -le 3 ]
then
 
 echo "
 $0 missing file operand.
 
 Usage: $0 <Width> <Height> <Directory> OutputImage
 The script creates a collage sized height x width out of the images in the directory specified. 
The resulting collage will be written as a jpg file into the Outputfile specified.
 
e.g. collageCreator.sh 640 480 images/ collage.jpg
 
will create a 640x480 image called collage.jpg and use the photos in the images directory to create a collage in it.
 
"
 exit 0
fi
 
# We assign the command line parameters to variables
 
width=$1
height=$2
SourceDirectory=$3
OutputImage=$4
 
# First we create a blank file for the collage
 
convert -size $1x$2 xc:black $OutputImage
 
# Then we search for all image files. I am just searching for jpg files as all my photos are jpg's
# and loop for each search result found and add it the collage
 
find $SourceDirectory -iname "thumbnail_*.jpg" -true | sed 's/ /\\ /g' |while read file
 
#awk "NR==$(($RANDOM % $(wc -l fi.txt| awk '{print $1}')))" fi.txt
 
# Now we start creating the collage
 
do
 
  # We have to seed the random number generator otherwise it tends to return the same set of results
  # We use the no of nanoseconds from the current date/time to seed it.
 
  RANDOM=`date '+%N'`
  x_location=`echo $(($RANDOM*$width/32767))`
  RANDOM=`date '+%N'`
  y_location=`echo $(($RANDOM*$height/32767))`
 
 
  echo $file
  # We insert the current image in the location selected by the random generator above in the output image
 
  composite -geometry 150x150+$x_location+$y_location "$file" $OutputImage $OutputImage
 
done
